package com.marry.producer.config;

import com.rabbitmq.client.ConnectionFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
 * @author ml
 * @create 2017-08-03--13:34
 */
@Configuration
@PropertySource(value = {"classpath:application.properties"})
public class RabbitMQ {

    @Autowired
    private Environment env;

    @Bean
    public ConnectionFactory connectionFactory() throws Exception{
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(env.getProperty("mq.host").trim());
        connectionFactory.setPort(Integer.parseInt(env.getProperty("mq.port").trim()));
        connectionFactory.setVirtualHost(env.getProperty("mq.vhost").trim());
        connectionFactory.setUsername(env.getProperty("mq.username").trim());
        connectionFactory.setPassword(env.getProperty("mq.password").trim());
        return connectionFactory;
    }

    @Bean
    public CachingConnectionFactory cachingConnectionFactory() throws Exception{
        CachingConnectionFactory cachingConnectionFactory=new CachingConnectionFactory(connectionFactory());
        return cachingConnectionFactory;
    }


    @Bean
    public RabbitTemplate rabbitTemplate() throws Exception{
        RabbitTemplate rabbitTemplate=new RabbitTemplate(cachingConnectionFactory());
        rabbitTemplate.setChannelTransacted(true);
        return rabbitTemplate;
    }

    @Bean
    public RabbitAdmin rabbitAdmin() throws Exception{
        return new RabbitAdmin(cachingConnectionFactory());
    }

    @Bean
    Queue queue(){
        String name=env.getProperty("mq.queue").trim();
        //是否持久化
        boolean durable= StringUtils.isNotBlank(env.getProperty("mq.queue.durable").trim())?Boolean.valueOf(env.getProperty("mq.queue.durable").trim()):true;

        //仅创建者可使用的私有队列 断开后自动删除

        boolean exclusive=StringUtils.isNotBlank(env.getProperty("mq.queue.exclusive").trim())?
                Boolean.valueOf(env.getProperty("mq.queue.exclusive").trim()):false;

        //当所有消费者客户端连接断开后，是否自动删除队列
        boolean autoDelete=StringUtils.isNotBlank(env.getProperty("mq.queue.autoDelete").trim())?
                Boolean.valueOf(env.getProperty("mq.queue.autoDelete").trim()):false;

        return new Queue(name,durable,exclusive,autoDelete);


    }

    @Bean
    TopicExchange exchange(){
        String name=env.getProperty("mq.exchange").trim();
        //是否持久化
        boolean durable=StringUtils.isNotBlank(StringUtils.trim(env.getProperty("mq.exchange.durable")))?
                Boolean.valueOf(StringUtils.trim(env.getProperty("mq.exchange.durable"))):false;
        //当所有消费客户端断开连接后，是否自动删除交换器
        boolean autoDelete=StringUtils.isNotBlank(env.getProperty("mq.exchange.autoDelete").trim())?
                Boolean.valueOf(env.getProperty("mq.exchange.autoDelete").trim()):false;

        return new TopicExchange(name,durable,autoDelete);
    }
    @Bean
    Binding binding(){
        String routeKey=StringUtils.trim(env.getProperty("mq.routekey"));
        return BindingBuilder.bind(queue()).to(exchange()).with(routeKey);
    }


}
